package insertData;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class InsertRandomData {
    private static final String DB_URL = "jdbc:mysql://localhost:3306/your_database";
    private static final String DB_USER = "your_username";
    private static final String DB_PASSWORD = "your_password";

    public static void main(String[] args) {
        try {
            Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);

            // Replace these values with the appropriate start and end years
            int startYear = 2020;
            int endYear = 2023;

            Random random = new Random();
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

            for (int year = startYear; year <= endYear; year++) {
                String tableName = "uf_t1_" + year;
                for (int i = 0; i < 14000000; i++) {
                    String modecreatedate = generateRandomDate(year, random, dateFormat);
                    String wb = generateRandomString(random, 5);
                    insertDataIntoTable(connection, tableName, modecreatedate, wb);
                }
                System.out.println("Inserted data into " + tableName);
            }

            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static String generateRandomDate(int year, Random random, DateFormat dateFormat) {
        int month = random.nextInt(12) + 1;
        int day = random.nextInt(28) + 1; // Assume all months have max 28 days for simplicity
        return year + "-" + String.format("%02d", month) + "-" + String.format("%02d", day);
    }

    public static String generateRandomString(Random random, int length) {
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(characters.length());
            sb.append(characters.charAt(index));
        }
        return sb.toString();
    }

    public static void insertDataIntoTable(Connection connection, String tableName, String modecreatedate, String wb) throws SQLException {
        String insertQuery = "INSERT INTO " + tableName + " (modecreatedate, wb) VALUES (?, ?)";
        try (PreparedStatement preparedStatement = connection.prepareStatement(insertQuery)) {
            preparedStatement.setString(1, modecreatedate);
            preparedStatement.setString(2, wb);
            preparedStatement.executeUpdate();
        }
    }
}
